package com.school.dao;

import com.school.model.RechargeRecord;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RechargeRecordDao {

    private final JdbcTemplate jdbcTemplate;

    public RechargeRecordDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void save(RechargeRecord record) {
        String sql = "INSERT INTO RechargeRecord (rechargeCode, amount, status) VALUES (?, ?, ?)";
        jdbcTemplate.update(sql, record.getRechargeCode(), record.getAmount(), record.getStatus());
    }

    public List<RechargeRecord> findAll() {
        String sql = "SELECT * FROM RechargeRecord";
        return jdbcTemplate.query(sql, (rs, rowNum) -> {
            RechargeRecord record = new RechargeRecord();
            record.setRechargeCode(rs.getString("rechargeCode"));
            record.setAmount(rs.getInt("amount"));
            record.setStatus(rs.getString("status"));
            record.setTimestamp(rs.getTimestamp("timestamp"));
            return record;
        });
    }
}